<?php
namespace Admin\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function _initialize(){
    
        if(!session('admins')){
            $this->error('无权限','http://localhost:8080/study/study/index.php/Home/Index/index.html');
            exit;
        }
    }
    
    public function users()
    {
        $usersmodel = M('Users');
        $local_users = $usersmodel->order('uid desc')->select();
        $this->users = $local_users;
        $this->display();
    }

    public function deluser($id)
    {
        // 生成一个Users模型
        $usersModel = M('Users');
        // 调用模型的方法删除数据
        // $usersModel -> delete($id);
        // delete from think_users where id=$id
        if ($usersModel->delete($id)) {
            // 删除成功
            $this->success('删除成功','users');
        } else {
            $this->error('删除失败');
            // 删除失败
        }
    }

    public function updateUserInfo($id)
    {
        $userModel = m('Users');
        $this->user = $userModel->find($id);
        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }

    public function updateUser()
    {
        // $data = $_POST;
        // $data = I(post.);
        // create(); 利用post数据创建一个对象
        $userModel = m('Users');
        $userModel->create();
        $userModel->regtime = date("Y-m-d H:i:s", time());
        if ($_FILES['photo']['error'] == 0) {
            $photoinfo = $this->upload();
            $userModel->photo = $photoinfo['photo']['savename'];
        }
        
        if ($userModel->save()) {
            $this->success('修改成功','users');
        } else {
            $this->error('修改失败');
        }
    }

    public function userAdd()
    {
        $this->display();
    }

    public function addUser()
    {
        $userModel = m('Users');
        if ($userModel->create()) {
            /*
             * if($_FILES['photo']['error']==0){
             * $photoinfo = $this->upload();
             * $userModel->photo = $photoinfo['photo']['savename'];
             * }
             */
            $userModel->regtime = date("Y-m-d H:i:s", time());
            
            if ($_FILES['photo']['error'] == 0) {
                $photoinfo = $this->img();
                $userModel->photo = $photoinfo;
            }
            
            if ($userModel->add()) {
                $this->success('添加成功','users');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->error($userModel->getError());
        }
    }

    public function upload()
    {
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array(
            'jpg',
            'gif',
            'png',
            'jpeg'
        ); // 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
                                                       // $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        // 上传文件
        $info = $upload->upload();
        if (! $info) { // 上传错误提示错误信息
            $this->error($upload->getError());
        } else { // 上传成功
                 // $this->success('上传成功！');
            return $info;
        }
    }

    public function img()
    {
        if ($_FILES['photo']['error'] == 0) {
            $photoinfo = $this->upload();
            $filePath = "./Public/Uploads/photo/";
            $oldFileName = $photoinfo['photo']['savename'];
            $oldFileFullName = $filePath . "\\" . $oldFileName;
            $newFileName = 'thumb_' . $oldFileName;
            $newFileFullName = $filePath . "\\" . $newFileName;
            // $imgFile = $photoinfo['photo']['rootpath'].'\\'.$photoinfo['photo']['savename'];
            $image = new \Think\Image();
            $image->open($oldFileFullName);
            $image->thumb(100, 100)->save($newFileFullName);
            return $newFileName;
        }
    }

    public function groups()
    {
        $groupsmodel = M('groups');
        $local_groups = $groupsmodel->order('gid')->select();
        $this->groups = $local_groups;
        $this->display();
    }

    public function delgroup($gid)
    {
        // 生成一个Users模型
        $groupModel = M('groups');
        // 调用模型的方法删除数据
        // $usersModel -> delete($id);
        // delete from think_users where id=$id
        if ($groupModel->delete($gid)) {
            // 删除成功
            $this->success('删除成功','groups');
        } else {
            $this->error('删除失败');
            // 删除失败
        }
    }

    public function updateGroupInfo($gid)
    {
        $groupModel = m('groups');
        $this->groups = $groupModel->find($gid);
        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }

    public function updateGroup()
    {
        // $data = $_POST;
        // $data = I(post.);
        // create(); 利用post数据创建一个对象
        $GroupModel = m('groups');
        $GroupModel->create();
        
        if ($_FILES['logo']['error'] == 0) {
            $photoinfo = $this->upload();
            $GroupModel->logo = $photoinfo['logo']['savename'];
        }
        
        if ($GroupModel->save()) {
            $this->success('修改成功','groups');
        } else {
            $this->error('修改失败');
        }
    }

    public function addGruop()
    {
        $GroupModel = m('Groups');
        if ($GroupModel->create()) {
            /*
             * 这img搞事情啊！！
             * if($_FILES['photo']['error']==0){
             * $photoinfo = $this->upload();
             * $userModel->photo = $photoinfo['photo']['savename'];
             * }
             */
            $GroupModel->createtime = date("Y-m-d H:i:s", time());
            
            /*
             * if ($_FILES['logo']['error'] == 0) {
             * $photoinfo = $this->upload();
             * $GroupModel->logo = $photoinfo;
             * }
             */
            if ($_FILES['logo']['error'] == 0) {
                $photoinfo = $this->upload();
                $GroupModel->logo = $photoinfo['logo']['savename'];
            }
            
            if ($GroupModel->add()) {
                $this->success('添加成功','groups');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->error($GroupModel->getError());
        }
    }

    
    public function groupInfo($gid)
    {
           $groupModel = m('groups');
        $this->groups = $groupModel->find($gid);
    
        $memberModel = m('members');
        $SearchGid['gid'] = $gid;
        $SearchGid['state'] = 1;
        $local_members = $memberModel->order('gid')->join('users ON users.stunum=members.stunum')
        ->where($SearchGid)
        ->select();
        $this->members = $local_members;
        
        //坐到这里了
        
        $memberShenpiModel = m('members');

        $local_Shenpimembers = $memberShenpiModel->join('users ON users.stunum=members.stunum')->join('groups ON groups.gid=members.gid')->where('state=0')->order('members.stunum')->select();
        $this->shenpis = $local_Shenpimembers;
        
        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }
   
    public function cancelGroupLeader($meid)
    {
        $MembersModel = m('members');
        $MembersModel->create();
          $SearchMeid['meid'] = $meid;
        $MembersModel->where( $SearchMeid)->state=0;
        if ($MembersModel->save()) {
            // 删除成功
            $this->success('撤销成功');
        } else {
            $this->error('撤销失败');
            // 删除失败
        }
    }

    public function setGroupLeader($meid){
    
        $MembersModel=m('members');
        $MembersModel->create();
        $SearchMeid['meid'] = $meid;
        $MembersModel->where( $SearchMeid)->state=1;
        if ($MembersModel->save()) {
            // 删除成功
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
            // 删除失败
        }
    }
    
    public function delGroupMember($meid)
    {
        $MembersModel = m('members');
        
        if ($MembersModel->delete($meid)) {
            // 删除成功
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
            // 删除失败
        }
    }

    public function memberAdd(){
        
        $this -> display();
    }
    
    public function doMemberAdd()
    {
        $MembersModel = m('members');
        $MembersModel->create();
        if ($MembersModel->add()) 
        {
            $this->success('添加成功');
        } else
        {
            $this->error('添加失败');
        }
    }

    
    
    
    public function practices()
    {
        $practicesmodel = m('practices');
        $local_practices = $practicesmodel->order('pid desc')->select();
        $this->practices = $local_practices;
        $this->display();
    }

    public function addPractice()
    {
        $PracticeModel = m('practices');
        if ($PracticeModel->create()) {
            /*
             * 这img搞事情啊！！
             * if($_FILES['photo']['error']==0){
             * $photoinfo = $this->upload();
             * $userModel->photo = $photoinfo['photo']['savename'];
             * }
             */
            $PracticeModel->pdate = date("Y-m-d H:i:s", time());
            
            /*
             * if ($_FILES['logo']['error'] == 0) {
             * $photoinfo = $this->upload();
             * $PracticeModel->logo = $photoinfo;
             * }
             */
            if ($_FILES['pfile']['error'] == 0) {
                $photoinfo = $this->uploadfile();
                $PracticeModel->pfile = $photoinfo['pfile']['savename'];
            }
            
            if ($PracticeModel->add()) {
                $this->success('添加成功','practices');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->error($PracticeModel->getError());
        }
    }

    public function updatePracticeInfo($pid)
    {
        $PracticeModel = m('practices');
        $this->practices = $PracticeModel->find($pid);
        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }

    public function delpractice($pid)
    {
        // 生成一个Users模型
        $practiceModel = M('practices');
        // 调用模型的方法删除数据
        // $usersModel -> delete($id);
        // delete from think_users where id=$id
        if ($practiceModel->delete($pid)) {
            // 删除成功
            $this->success('删除成功','practices');
        } else {
            $this->error('删除失败');
            // 删除失败
        }
    }
    
    public function updatePractice()
    {
        // $data = $_POST;
        // $data = I(post.);
        // create(); 利用post数据创建一个对象
        $PracticeModel = m('practices');
        $PracticeModel->create();
        // $PracticeModel->ptime=date("Y-m-d H:i:s" ,time());
        $PracticeModel->pdate = date("Y-m-d H:i:s", time());
        
        if ($_FILES['pfile']['error'] == 0) {
            $fileinfo = $this->uploadfile();
            $PracticeModel->pfile = $fileinfo['pfile']['savename'];
        }
        
        if ($PracticeModel->save()) {
            $this->success('修改成功','practices');
        } else {
            $this->error('修改失败');
        }
    }

 public function practiceInfo($pid)
    {
        $practiceModel = m('practices');
        $this->practices = $practiceModel->find($pid);
    
        $stupracModel = m('stuprac');
        $SearchPid['stuprac.pid'] = $pid;
        $SearchPid['state'] = 1;
        $local_stupracs = $stupracModel->join('practices ON practices.pid=stuprac.pid')->join('users ON users.stunum=stuprac.stunum')->order('users.stunum')
        ->where($SearchPid)
        ->select();
        $this->stuprac = $local_stupracs;
        
        $stupracShenpiModel = m('stuprac');
        $SearchPid['stuprac.pid'] = $pid;
        $SearchPid['state'] = 0;
        $local_stupracShenpiModel = $stupracShenpiModel->join('practices ON practices.pid=stuprac.pid')->join('users ON users.stunum=stuprac.stunum')->order('users.stunum')
        ->where($SearchPid)
        ->select();
        
        $this->shenpis = $local_stupracShenpiModel;
        
        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }
     
    public function cancelPracticeSuccess($spid)
    {
        $StupracModel = m('Stuprac');
        $pppid['spid']=$spid;
        $StupracModel->where($pppid)->state=0;
        if ($StupracModel->save()) {
            // 删除成功
            $this->success('撤销成功');
        } else {
            $this->error('撤销失败');
            // 删除失败
        }
    }
    
    public function setPracticeLeader($spid){
    
        $StupracModel=m('Stuprac');
         $pppid['spid']=$spid;
        $StupracModel->where($pppid)->state=1;
        if ($StupracModel->save()) {
            // 删除成功
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
            // 删除失败
        }
    }
    
    public function delPracticeStuprac($spid)
    {
        $StupracModel = m('stuprac');
    
        if ($StupracModel->delete($spid)) {
            // 删除成功
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
            // 删除失败
        }
    }
    
    public function stupracAdd(){
    
        $this -> display();
    }
    
    public function doStupracAdd()
    {
        $MembersModel = m('stuprac');
        $MembersModel->create();
        if ($MembersModel->add())
        {
            $this->success('添加成功');
        } else
        {
            $this->error('添加失败');
        }
    }
    
    
    
    
    
    
    public function uploadfile()
    {
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array(
            'doc',
            'wps',
            'docx',
            'rar',
            'txt'
        ); // 设置附件上传类型
        $upload->rootPath = './Public/Uploads/file/'; // 设置附件上传根目录
                                                      // $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        // 上传文件
        $info = $upload->upload();
        if (! $info) { // 上传错误提示错误信息
            $this->error($upload->getError());
        } else { // 上传成功
                 // $this->success('上传成功！');
            return $info;
        }
    }

    public function setting()
    {
        $AdminsModel = m('Users');
        $this->admins = $AdminsModel->where('isadmin=1')->find();
        $this->display();
    }

    public function updateAdmin()
    {
        $AdminsModel = m('Users');
        $AdminsModel->create();
        
        if ($AdminsModel->save()) {
            $this->success('修改成功');
        } else {
            $this->error($AdminsModel->getError());
        }
    }

    public function newsAdd()
    {
        $this->display();
    }
    
    public function addNews()
    {
        $NewModel = m('news');
        if ($NewModel->create()) {
            /*
             * 这img搞事情啊！！
             * if($_FILES['photo']['error']==0){
             * $photoinfo = $this->upload();
             * $userModel->photo = $photoinfo['photo']['savename'];
             * }
             */
            $NewModel->ntime = date("Y-m-d H:i:s", time());
            /*
             * if ($_FILES['logo']['error'] == 0) {
             * $photoinfo = $this->upload();
             * $GroupModel->logo = $photoinfo;
             * }
            */
            if ($_FILES['pic']['error'] == 0) {
                $photoinfo = $this->upload();
                $NewModel->pic = $photoinfo['pic']['savename'];
            }
    
            if ($NewModel->add()) {
                $this->success('添加成功','newsLatest');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->error($NewModel->getError());
        }
    }
    
    public function newLatest()
    {
        $newsmodel = M('news');
        $local_news = $newsmodel->order('nid')->select();
        $this->news = $local_news;
        $this->display();
    }
    
    public function updateNewInfo($nid)
    {
        $newsModel = m('News');
        $this->news = $newsModel->find($nid);
        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }
    
    public function updateNew()
    {
        // $data = $_POST;
        // $data = I(post.);
        // create(); 利用post数据创建一个对象
        $NewModel = m('News');
        $NewModel->create();
        
        $NewModel->ntime = date("Y-m-d H:i:s", time());
      
        if ($_FILES['pic']['error'] == 0) {
            $photoinfo = $this->upload();
            $NewModel->pic = $photoinfo['pic']['savename'];
        }
        if ($NewModel->save()) {
            $this->success('修改成功','newLatest');
        } else {
            $this->error('修改失败');
        } 
    }
    

    public function delnew($nid)
    {
        // 生成一个Users模型
        $newModel = M('news');
        // 调用模型的方法删除数据
        // $usersModel -> delete($id);
        // delete from think_users where id=$id
        if ($newModel->delete($nid)) {
            // 删除成功
            $this->success('删除成功','newLatest');
        } else {
            $this->error('删除失败');
            // 删除失败
        }
    }
    
    public function newInfo($nid)
    {
        $NewModel = m('News');
        $this->news = $NewModel->find($nid);
        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }
    
    
}